package com.aimao.blog.config;

import com.aimao.blog.constant.ServerConst;
import com.aimao.blog.pojo.Admin;
import com.aimao.blog.service.AccountAdminService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @Author PuaChen
 * @Create 2018-07-26 16:23
 */
@WebListener
public class SessionListener implements HttpSessionListener {

    @Autowired
    private AccountAdminService adminService;

    @Override
    public void sessionCreated(HttpSessionEvent se) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        Object admin = session.getAttribute(ServerConst.CURRENT_USER);
        if (admin != null && admin instanceof Admin) {
            if (((Admin) admin).getId() != null) {
                adminService.setLastLogoutTime((Admin) admin);
            }
        }
    }
}
